<!-- 设备 》 设备类别 -->

<template>
  <CommonBody
    hasTree
    :tree="$root.device_category_tree"
    :field="$root.device_category_TREE_FIELD"
    diy
    flexColumn
    @change="setOne"
  >
    <!-- 结构树插槽 -->
    <template #treeBottom>
      <BtnAdd word="分类" mutation="add/openAdd" />
    </template>

    <!-- 展示卡片 -->
    <EasyCard title="分类信息">
      <FormEdit />
    </EasyCard>

    <!-- 表格卡片 -->
    <EasyCard title="动态属性" height="calc(100% - 257px)">
      <ChildSearch />

      <ChildTable />
    </EasyCard>

    <!-- 删除弹窗 -->
    <WebModal word="分类" />

    <!-- 新增弹窗 -->
    <WebModal path="add" word="分类" small>
      <FormAdd />
    </WebModal>

    <!-- 动态属性弹窗 -->
    <WebModal path="child" word="动态属性" small>
      <ChildForm />
    </WebModal>
  </CommonBody>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import store from './store'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'DeviceCategory',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixStore(store)],

  /**
   * 组件导入 (Abc.vue + Abc/index.vue)
   */
  ...CORE.components(require.context('./', true, /vue$/)),

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听结构树就绪
     */
    '$root.device_category_readyTree': {
      immediate: true,
      handler(ready) {
        if (ready) {
          const { one: { id }, $root: { device_category_tree: tree, device_category_treeMap: treeMap } } = this
          const chosen = id ? treeMap[id] : tree[0] || {}
          this.param.treeId = chosen.id
          this.setOne(chosen)
        }
      },
    },

    /**
     * 监听单项
     */
    one(one) {
      if (one.id) {
        const { child_param, child_INIT_ONE, child_startList } = this
        child_param.assetTypeId = child_INIT_ONE.assetTypeId = one.id
        child_startList()
      }
    },

    /**
     * 监听子模块的新增就绪
     */
    add_readyAdd(ready) {
      ready && this.$root.device_category_startTree()
    },

    /**
     * 监听修改就绪
     */
    readyEdit(ready) {
      ready && this.$root.device_category_startTree()
    },

    /**
     * 监听删除就绪
     */
    readyDel(ready) {
      ready && this.$root.device_category_startTree()
    },
  },
}
</script>
